package com.kaisu.blog.infrastructure.user.dao;

import com.kaisu.blog.infrastructure.user.UserPO;
import lombok.NonNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author kaisu
 */
@Repository
public interface UserPODAO extends JpaRepository<UserPO, String> {
    @EntityGraph(value = "user-follow", attributePaths = {"roles", "followers", "followings"}, type = EntityGraph.EntityGraphType.LOAD)
    Optional<UserPO> findByUsernameOrEmailOrPhone(String username, String email, String phone);

    @EntityGraph(value = "user-follow", attributePaths = {"roles", "followers", "followings"}, type = EntityGraph.EntityGraphType.LOAD)
    List<UserPO> findAllByPhoneIn(Collection<String> names);

    @EntityGraph(value = "user-follow", attributePaths = {"roles", "followers", "followings"}, type = EntityGraph.EntityGraphType.LOAD)
    Page<UserPO> findAll(Specification<UserPO> specification, Pageable pageable);

    @NonNull
    @Override
    @EntityGraph(value = "user-follow", attributePaths = {"roles", "followers", "followings"}, type = EntityGraph.EntityGraphType.LOAD)
    List<UserPO> findAllById(@NonNull Iterable<String> strings);

    @EntityGraph(value = "user-follow", attributePaths = {"roles", "followers", "followings"}, type = EntityGraph.EntityGraphType.LOAD)
    Optional<UserPO> findByUsername(String username);
}
